//------------------------------------------------------------------------------ 
// <copyright file="JResizable.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
namespace FmjQuery
{
    using System.ComponentModel;
    using System.Text;
    using System.Web.UI;

    /// <summary>
    /// Resizable extender control
    /// </summary>
    [ToolboxData("<{0}:JResizable runat=server Speed=normal />")]
    [DefaultProperty("TargetControlID")]
    [NonVisualControl]
    public class JResizable : JQueryExtendControl, IResizeDimension
    {
        #region Fields

        /// <summary>
        /// List of yets
        /// </summary>
        private Yets tyets;
        #endregion

        #region Costruct
        /// <summary>
        /// Initializes a new instance of the JResizable class.
        /// </summary>
        public JResizable()
        {
            this.IsRenderControl = false;
        }
        #endregion

        #region Property

        /// <summary>
        /// Gets or sets a value indicating whether resize these elements synchronous when resizing
        /// </summary>
        public bool AlsoResize 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether animate resize
        /// </summary>
        public bool Animate 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets speed of animation
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [DefaultValue("normal")]
        public SpeedType Speed
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether resizing is constrained by the original aspect ratio. Otherwise a custom aspect ratio can be specified, such as 9 / 16, or 0.5
        /// </summary>
        public bool AspectRatio 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether automatically hides the handles except when the mouse hovers over the element
        /// </summary>
        public bool AutoHide 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets id of container control
        /// </summary>
        public string ContainerID 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether container control is client
        /// </summary>
        public bool IsClientContainer 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets tolerance, in milliseconds, for when resizing should start. If specified, resizing will not start until after mouse is moved beyond duration
        /// </summary>
        public int Delay 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets tolerance, in pixels, for when resizing should start. If specified, resizing will not start until after mouse is moved beyond distance. This can help prevent unintended resizing when clicking on an element
        /// </summary>
        public int Distance 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether a semi-transparent helper element is shown for resizing
        /// </summary>
        public bool Ghost 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets value of step for horizontal resize
        /// </summary>
        public int HorizontalStep 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a velue of step for vertical resize
        /// </summary>
        public int VerticalStep
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets list of yet
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Yets Yets 
        { 
            get
            {
                if (this.tyets == null)
                {
                    this.tyets = new Yets();
                }

                return this.tyets;
            }
        }

        /// <summary>
        /// Gets or sets the css class that will be added to a proxy element to outline the resize during the drag of the resize handle. Once the resize is complete, the original element is sized
        /// </summary>
        public string Helper 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets max of height
        /// </summary>
        public int MaxHeight
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets max of width
        /// </summary>
        public int MaxWidth 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets min of height
        /// </summary>
        public int MinHeight 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets min of width
        /// </summary>
        public int MinWidth 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets opacity
        /// </summary>
        public float Opacity 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets or sets a value indicating whether prevent Safari's default resizing function on textareas
        /// </summary>
        public bool PreventDefault 
        { 
            get; 
            set; 
        }
        #endregion

        #region Overrides of JQueryExtendControl
        /// <summary>
        /// Return list of webResource
        /// </summary>
        /// <returns>web resources</returns>
        public override JWebSources GetResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public override CssResources GetCssResources()
        {
            return null;
        }

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public override ClientNameSpaces GetClientNameSpace()
        {
            return null;
        }

        /// <summary>
        /// Create jQuery control
        /// </summary>
        public override void CreateJControl()
        {
            if (this.Enabled)
            {
                this.Manager.Resizable = true;

                // create script
                StringBuilder builder = new StringBuilder();

                builder.AppendFormat("$('#{0}').resizable(", this.TargetControlClientID);
                builder.Append("{");

                builder.AppendFormat("animateDuration: '{0}'", this.Speed);

                if (this.AlsoResize)
                {
                    builder.Append(",alsoResize: true");
                }

                if (this.Animate)
                {
                    builder.Append(",animate: true");
                }

                if (this.AspectRatio)
                {
                    builder.Append(",aspectRatio: true");
                }

                if (this.AutoHide)
                {
                    builder.Append(",autoHide: true");
                }

                // container
                if (!string.IsNullOrEmpty(this.ContainerID))
                {
                    builder.AppendFormat(",containment: '#{0}'", this.IsClientContainer ? this.ContainerID : FindControl(this.ContainerID).ClientID);
                }

                if (!this.Delay.Equals(0))
                {
                    builder.AppendFormat(",delay: {0}", this.Delay);
                }

                if (!this.Distance.Equals(0))
                {
                    builder.AppendFormat(",distance: {0}", this.Distance);
                }

                if (this.Ghost)
                {
                    builder.Append(",ghost: true");
                }

                // create grid step
                if (!this.HorizontalStep.Equals(0) && !this.VerticalStep.Equals(0))
                {
                    if (this.HorizontalStep.Equals(0) && !this.VerticalStep.Equals(0))
                    {
                        this.HorizontalStep = 1;
                    }

                    if (!this.HorizontalStep.Equals(0) && this.VerticalStep.Equals(0))
                    {
                        this.VerticalStep = 1;
                    }

                    builder.AppendFormat(",grid: [{0},{1}]", this.HorizontalStep, this.VerticalStep);
                }

                // create yet
                if (this.Yets.Count > 0)
                {
                    builder.Append(",handles: '");

                    bool first = true;

                    foreach (CardialiPoint yet in this.Yets)
                    {
                        if (first)
                        {
                            builder.Append(",");
                        }

                        builder.Append(yet.ToString());
                        first = false;
                    }

                    builder.Append("'");
                }

                if (!string.IsNullOrEmpty(this.Helper))
                {
                    builder.AppendFormat(",helper: '{0}'", this.Helper);
                }

                if (this.MaxHeight > 0)
                {
                    builder.AppendFormat(",maxHeight: {0}", this.MaxHeight);
                }

                if (this.MaxWidth > 0)
                {
                    builder.AppendFormat(",maxwidth: {0}", this.MaxWidth);
                }

                if (this.MinHeight > 0)
                {
                    builder.AppendFormat(",minHeight: {0}", this.MinHeight);
                }

                if (this.MinWidth > 0)
                {
                    builder.AppendFormat(",minWidth: {0}", this.MinWidth);
                }

                if (!this.Opacity.Equals(0.0f))
                {
                    builder.AppendFormat(",opacity: {0}", this.Opacity.ToString().Replace(",", "."));
                }

                if (this.PreventDefault)
                {
                    builder.AppendFormat(",preventDefault: true");
                }

                builder.Append("});");

                Utility.RegisterStartUpScript(this.Page, string.Concat("Resize_", this.ClientID), builder.ToString());
            }
        }
        #endregion
    }
}